package com.stu.management.service.impl;


import com.github.pagehelper.PageHelper;
import com.stu.management.mapper.MenuMapper;
import com.stu.management.mapper.MiddleMapper;
import com.stu.management.mapper.RoleStuMapper;
import com.stu.management.mapper.UsersStuMapper;
import com.stu.management.pojo.*;
import com.stu.management.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {
    @Resource(name = "usersStuMapper")
    private UsersStuMapper usersDao;
    @Autowired
    private RoleStuMapper roleDao;
    @Autowired
    private MiddleMapper middleMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public UsersStu login(String username, String password) {
//        UsersStu login = usersDao.login(username, password);
        UsersStuExample usersStuExample = new UsersStuExample();
        usersStuExample.createCriteria()
                .andLoginnameEqualTo(username)
                .andPasswordEqualTo(password);
        List<UsersStu> login = usersDao.selectByExample(usersStuExample);
        if (login.isEmpty()) {
            return null;
        }
        Integer roleId = login.get(0).getRoleid();
        MiddleExample middleExample = new MiddleExample();
        middleExample.createCriteria()
                .andRoleidEqualTo(roleId);
        List<Middle> middles = middleMapper.selectByExample(middleExample);
        List<Menu> menuList = new ArrayList<>();
        middles.forEach(c->menuList.add(menuMapper.selectByPrimaryKey(c.getMenuid())));
        System.out.println("id对应菜单列表：" + menuList.toString());
        ArrayList<Menu> newMenuList = new ArrayList<>();
        for (Menu menu :
                menuList) {
            if (0 == menu.getUpmenuid()) {
                List<Menu> secondList = new ArrayList<>();
                for (Menu second :
                        menuList) {
                    if (menu.getMenuid() == second.getUpmenuid()) {
                        secondList.add(second);
                    }
                }
                menu.setSecondMenuList(secondList);
                newMenuList.add(menu);
            }
        }
        System.out.println("roleId为：" + roleId + "角色对应权限" + newMenuList);
        RoleStu roleStu = roleDao.selectByPrimaryKey(roleId);
        roleStu.setMenuList(newMenuList);
        login.get(0).setRoleStu(roleStu);
        return login.get(0);
    }

    @Override
    public List<UsersStu> getUsersList(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<UsersStu> usersStus = usersDao.selectByExample(null);
        return usersStus;
    }

    @Override
    public int total() {
        return (int) usersDao.countByExample(null);
    }


    @Override
    public int insertUser(UsersStu user) {
        return usersDao.insert(user);
    }

    @Override
    public int updateUser(UsersStu user) {
        return usersDao.updateByPrimaryKey(user);
    }

    @Override
    public UsersStu findById(int userId) {
        return usersDao.selectByPrimaryKey(userId);
    }

    @Override
    public int deleteById(Integer userId) {
        return usersDao.deleteByPrimaryKey(userId);
    }
}
